package com.coscon.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.logging.Logger;

/**
 * 控制电脑系统音量 win平台
 * <p/>
 * 约定在应用根目录下的 temp 目录中放置3个vbs文件
 * volumeMute.vbs：用于静音
 * volumeAdd.vbs：增加音量
 * volumeMinus.vbs：减小音量
 * 文件以及文件的内容采用 Java 代码动态生成，不存在时则新建，存在时则直接调用
 * <p>
 * 0：静音/取消静音    1：增加音量  2：减小音量
 */
@Slf4j
public class SystemVolumeByWinUtils {

    //临时目录
    private static String tempDir = SystemUtils.JAVA_IO_TMPDIR;

    public static void controlSystemVolume(int type) {
        try {
            if (type < 0 || type > 2) {
                log.info("type 参数只能是0,1,2");
                return;
            }
            /**tempFile：vbs 文件
             * vbsMessage：vbs 文件的内容*/
            String vbsMessage = "";
            File tempFile = null;
            Runtime runtime = Runtime.getRuntime();
            switch (type) {
                case 0:
                    tempFile = new File(tempDir, "volumeMute.vbs");
                    vbsMessage = !tempFile.exists() ? "CreateObject(\"Wscript.Shell\").Sendkeys \"棴\"" : "";
                    break;
                case 1:
                    tempFile = new File(tempDir, "volumeAdd.vbs");
                    vbsMessage = !tempFile.exists() ? "CreateObject(\"Wscript.Shell\").Sendkeys \"棷\"" : "";
                    break;
                case 2:
                    tempFile = new File(tempDir, "volumeMinus.vbs");
                    vbsMessage = !tempFile.exists() ? "CreateObject(\"Wscript.Shell\").Sendkeys \"棶\"" : "";
                    break;
                default:
                    return;
            }
            /**
             * 当3个vbs文件不存在时，则创建它们，应用默认编码为 utf-8 时，创建的 vbs 脚本运行时报错
             * 于是使用 OutputStreamWriter 将 vbs 文件编码改成gbd就正常了
             */
            if (!tempFile.exists() && !"".equals(vbsMessage)) {
//                if (!tempFile.getParentFile().exists()) {
//                    tempFile.getParentFile().mkdirs();
//                }
                try {
                    boolean folderCreated = tempFile.getParentFile().mkdirs();
                    // if dir wasn't created this time, and doesn't exist
                    if (!folderCreated && !tempFile.getParentFile().exists()) {
                        throw new IOException("Unable to create path");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

                try {
                    boolean fileCreated = tempFile.createNewFile();
                    // if dir wasn't created this time, and doesn't exist
                    if (!fileCreated && !tempFile.exists()) {
                        throw new IOException("Unable to create file");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

                FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "GBK");
                outputStreamWriter.write(vbsMessage);
                outputStreamWriter.flush();
                outputStreamWriter.close();
                log.info("vbs 文件不存在，新建成功：" + tempFile.getAbsolutePath());
            }
            runtime.exec("wscript " + tempFile.getAbsolutePath()).waitFor();
            log.info("音量控制完成.");
        } catch (IOException e) {
            log.warn(e.getMessage(), e);
        } catch (InterruptedException e) {
            log.warn(e.getMessage(), e);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        SystemVolumeByWinUtils.controlSystemVolume(0);
    }
}
